/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Stroke;
import java.util.ArrayList;
import type.hitoType;

/**
 *
 * @author JorgeLuis
 */
public class hitoModel {
    
    
    private String   name    = "";
    private String   alias   = "";
    private int      srcImage= -1;
    private boolean  seeName = true;
    
    private int      id      = -1;

    private Point    position;
    
    private hitoType type              = hitoType.HITO;    
    
    private Color    colorEdge         = new Color(0, 255, 255);
    private Color    colorFill         = new Color(255, 0, 255);
    
    private Color    colorEdgeOriginal = colorEdge;
    private Color    colorFillOriginal = colorFill;
     
    private Color colorEdgeContrary = new Color(
            Math.abs(colorEdge.getRed()   -255),
            Math.abs(colorEdge.getBlue()  -255),
            Math.abs(colorEdge.getGreen() -255)
            );
    
    private Color colorFillContrary = new Color(
            Math.abs(colorFill.getRed()     -255),
            Math.abs(colorFill.getBlue()    -255),
            Math.abs(colorFill.getGreen()   -255));
    
    private BasicStroke borderWidth = new BasicStroke(1.0f);
    
    //_____________________________________________________________
    
    private ArrayList<roadModel> roads = new ArrayList<roadModel>();
    private Polygon area = new Polygon();
    private ArrayList<hitoModel> vertices = null;
    private boolean seeVertices = false;
    private hitoModel selectVertex;

    public hitoModel() {
    }

    public hitoModel(Point position) {
        this.position = position;
        this.setType(hitoType.HITO);
    }

    public String getAlias() {
        return alias;
    }

    public Polygon getArea() {
        return area;
    }

    public BasicStroke getEdgeWidth() {
        return borderWidth;
    }

    public Color getColorEdge() {
        return colorEdge;
    }

    public Color getColorEdgeContrary() {
        return colorEdgeContrary;
    }

    public Color getColorEdgeOriginal() {
        return colorEdgeOriginal;
    }

    public Color getColorFill() {
        return colorFill;
    }

    public Color getColorFillContrary() {
        return colorFillContrary;
    }

    public Color getColorFillOriginal() {
        return colorFillOriginal;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Point getPosition() {
        return position;
    }

    public ArrayList<roadModel> getRoads() {
        return roads;
    }

    public int getSrcImage() {
        return srcImage;
    }

    public hitoType getType() {
        return type;
    }

    public ArrayList<hitoModel> getVertices() {
        return vertices;
    }

    public boolean isSeeName() {
        return seeName;
    }

    public boolean isSeeVertices() {
        return seeVertices;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public void setArea(Polygon area) {
        this.area = area;
    }

    public void setEdgeWidth(BasicStroke borderWidth) {
        this.borderWidth = borderWidth;
    }

    public void setColorEdge(Color colorEdge) {
        
        this.colorEdge = colorEdge;
        
    }

    public void setColorEdgeContrary(Color colorEdgeContrary) {
        this.colorEdgeContrary = colorEdgeContrary;
    }

    public void setColorEdgeOriginal(Color colorEdgeOriginal) {
        this.colorEdgeOriginal = colorEdgeOriginal;
        
        this.colorEdgeContrary = new Color(
                Math.abs(colorEdgeOriginal.getRed()  -255),
                Math.abs(colorEdgeOriginal.getBlue() -255),
                Math.abs(colorEdgeOriginal.getGreen()-255)
                );
    }

    public void setColorFill(Color colorFill) {
        this.colorFill = colorFill;
    }

    public void setColorFillContrary(Color colorFillContrary) {
        this.colorFillContrary = colorFillContrary;
    }

    public void setColorFillOriginal(Color colorFillOriginal) {
        this.colorFillOriginal = colorFillOriginal;
    }

    public void setId(int id) {
        this.id = id;
        this.name = String.valueOf(id);
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPosition(Point position) {
        
        int translateX  =   (position.x - this.position.x );
        int translateY  =   (position.y - this.position.y );
                 
        this.translate(position);
        int lenRoad = this.roads.size();
        
        for (int i = 0; i < lenRoad; i++) {
            
            if(this.roads.get(i).getHitoOne().getId() == this.id){               
        
               this.roads.get(i).movePositionOne(translateX, translateY);
            }            
            else{
                this.roads.get(i).movePositionTwo(translateX, translateY);
            }
            
            this.roads.get(i).setSeeDistance(true);
        }
        this.position = position;
    }

    public void setRoads(ArrayList<roadModel> roads) {
        this.roads = roads;
    }

    public void setSeeName(boolean seeName) {
        this.seeName = seeName;
    }

    public void setSeeVertices(boolean seeVertices) {
        this.seeVertices = seeVertices;
        if(seeVertices){
            this.assignVertices();
        }
        else{
            this.vertices = null;
            this.selectVertex = null;
        }
    }

    public void setSrcImage(int srcImage) {
        this.srcImage = srcImage;
    }

    public void setType(hitoType tipo) {
        this.type = tipo;
        
        switch(tipo){
            case HITO:   
                area = new Polygon();
                System.out.println("position "+position);
                area.addPoint((int)position.getX() - 3,(int) position.getY() - 3);
                area.addPoint((int)position.getX() + 3,(int) position.getY() - 3);
                area.addPoint((int)position.getX() + 3,(int) position.getY() + 3);
                area.addPoint((int)position.getX() - 3,(int) position.getY() + 3);
                break;
        }
    }

    public void setVertices(ArrayList<hitoModel> vertices) {
        this.vertices = vertices;
    }
    
    public boolean intersects(Point point){
        return area.intersects(point.x - 1.5, point.y - 1.5, 3, 3);
    }
    
   
    
    public void assignVertices(){
        vertices = new ArrayList<hitoModel>();
        this.selectVertex = null;
        for (int i = 0; i < area.npoints; i++) {
            vertices.add(new hitoModel( new Point( area.xpoints[i], area.ypoints[i] )));
        }
    }
    
    public void drawn(Graphics2D g2){        
        
        Color colorOrigenal = g2.getColor();
        Stroke grosorOriginal = g2.getStroke();
        
        g2.setColor(colorFill);
        if(this.type == hitoType.HITO){
            g2.fillOval(
                    (int) area.getBounds().getMinX(),
                    (int) area.getBounds().getMinY(),
                    (int) area.getBounds().getWidth(),
                    (int) area.getBounds().getHeight()
            );
        }
        else{
            g2.fill(area);
        }
        
        
        g2.setColor(colorEdge);
        if(this.type == hitoType.HITO){
            g2.drawOval(
                    (int) area.getBounds().getMinX(),
                    (int) area.getBounds().getMinY(),
                    (int) area.getBounds().getWidth(),
                    (int) area.getBounds().getHeight()
            );
        }
        else{
            g2.draw(area);
        }
        
        
        if(seeName){
            g2.drawString(name,(int) area.getBounds().getCenterX(),(int) area.getBounds().getCenterY());
        }
        
        if(seeVertices && vertices != null && this.type != hitoType.HITO){
            
            for(int i = 0; i < vertices.size(); i++){
                vertices.get(i).drawn(g2);
            }
        }
        
        g2.setStroke(grosorOriginal);
        g2.setColor(colorOrigenal);
        
    }
    
    public void translate(Point point){
        
        int translateX  =   (point.x - this.position.x );
        int translateY  =   (point.y - this.position.y );
                
        area.translate(translateX, translateY);
//        if(vertices != null){
//            assignVertices();
//        }
    }

    public void removeRoad(int id) {
        
        for (int i = 0; i < this.roads.size(); i++) {
            
            if(this.roads.get(i).getId() == id){
                roads.remove(roads.get(i));
                i = roads.size();
            }
        }
    }

    public void addRoad(roadModel newRoad) {
        roads.add(newRoad);
    }
    
    public void moveVertice(Point point){
        
        
        if(vertices != null && this.selectVertex == null){
            
            for (int i = 0; i < vertices.size(); i++) {
                hitoModel vertex = vertices.get(i);
                System.out.println(vertex.getPosition());
                if( vertex.intersects(point)){
                    this.selectVertex = vertex;
                    this.selectVertex.setId(i);
                    i = vertices.size();
                }
            }
        }
        if(this.selectVertex != null){
            this.selectVertex.setPosition(point);
            area.xpoints[ this.selectVertex.getId()] = point.x;
            area.ypoints[ this.selectVertex.getId()] = point.y;
            area = new Polygon(area.xpoints, area.ypoints, area.npoints);
        }
        
    }

    public void setSelectVertex(hitoModel selectVertex) {
        this.selectVertex = selectVertex;
    }   
    
}
